package com.example.diabeco;


import java.util.Calendar;

import android.app.ActionBar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.ToggleButton;


public class PersonalDataActivity extends Activity {
//	//List Box Peso
//	private Spinner spinnerPeso; 
//	
//	//List Box Altura
//	private Spinner spinnerAltura; 
	
	//Date Picker
	private TextView editTextBirthDate;
	private int v_Year;
	private int v_Month;
	private int v_Day;
	static final int DATE_DIALOG_ID = 999;
	
	//Alias 2
	private TextView editTextNick2;
	private TextView textViewNick2;
	
	//Autocompletar Pais
	private AutoCompleteTextView autoCompleteCountry;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_personal_data);

	    // Nos aseguramos que la aplicacion corra en Honeycomb(3.0) o superior para usar el "ActionBar APIs"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Mostramos el boton de back en el "ActionBar"
//        	ActionBar actionBar = getActionBar();
//            actionBar.setDisplayHomeAsUpEnabled(true);
//            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
        
		//Listener del Date Dialog
 //		addListenerOnButton();
		
		//Autocomplete Pais
		setAutocompleteCountry();
	}

	
    @Override //Para que vuelva al Home
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.personal_data, menu);
		return true;
	}

	
	public void setAutocompleteCountry() {
		// Obtengo la referencia del Autocompletar del Pais
		autoCompleteCountry = (AutoCompleteTextView) findViewById(R.id.autoCompleteCountry);
		
		// Obtenego el Array de Paises
		String[] strCountries = getResources().getStringArray(R.array.lb_countries);
		
		// Creo el adaptador y lo seteo al  AutoCompleteTextView 
		ArrayAdapter<String> adapter = 
		        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strCountries);
		
		//Muestro la lista
		autoCompleteCountry.setAdapter(adapter);
	}
	
	
	
	public void onClickBirthDate(View view) {

		editTextBirthDate = (TextView) findViewById(R.id.editTextBirthDate);
		 
		editTextBirthDate.setOnClickListener(new OnClickListener() {
 
			@Override
			public void onClick(View v) {
 				showDialog(DATE_DIALOG_ID);
 			}
 
		});
    }
	
//	public void addListenerOnButton() {
//		 
//		editTextBirthDate = (TextView) findViewById(R.id.editTextBirthDate);
// 
//		editTextBirthDate.setOnClickListener(new OnClickListener() {
// 
//			@Override
//			public void onClick(View v) {
// 				showDialog(DATE_DIALOG_ID);
// 			}
// 
//		});
// 	}
		
    @Override //(by Palma)
    protected Dialog onCreateDialog(int id) {
		
	   // Agregamos la fecha actual y llamamos al DatePicketDialog
		final Calendar c = Calendar.getInstance();
		v_Year = c.get(Calendar.YEAR);
		v_Month = c.get(Calendar.MONTH);
		v_Day = c.get(Calendar.DAY_OF_MONTH);
		
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,  objDateSetListener,  v_Year, v_Month, v_Day);
        }
        return null;
    }
   
    private DatePickerDialog.OnDateSetListener objDateSetListener 
    		= new DatePickerDialog.OnDateSetListener() {
        
    	// when dialog box is closed, below method will be called.
    	public void onDateSet(DatePicker view, int selectedYear,
    						int selectedMonth, int selectedDay) {
    		
    				v_Year  = selectedYear;
    				v_Month = selectedMonth;
    				v_Day   = selectedDay;
    	 
    				// set selected date into textview
    				editTextBirthDate.setText(new StringBuilder().append(v_Day)
    				   .append("/").append(v_Month + 1).append("/").append(v_Year)
    				   .append(" "));
    	    	 
    			}
    };
    
    //Click en el Boton "Usuario Final"
    public void onToggleClicked(View view) {
    	editTextNick2 = (TextView) findViewById(R.id.editTextNick2);
    	textViewNick2 = (TextView) findViewById(R.id.textViewNick2);
    	// Is the toggle on?
        boolean v_On = ((ToggleButton) view).isChecked();
        
        if (v_On) {
            // Desactivo el Nick2
        	editTextNick2.setEnabled(false);
        	textViewNick2.setVisibility(View.GONE);
        } else {
            // Activo el Nick2
        	editTextNick2.setEnabled(true);
        	textViewNick2.setVisibility(View.VISIBLE);
        }
    }
    
//	public Spinner getSpinnerPeso() {
//		return spinnerPeso;
//	}
//
//	public void setSpinnerPeso(Spinner spinnerPeso) {
//		this.spinnerPeso = spinnerPeso;
//	}
//
//	public Spinner getSpinnerAltura() {
//		return spinnerAltura;
//	}
//
//	public void setSpinnerAltura(Spinner spinnerAltura) {
//		this.spinnerAltura = spinnerAltura;
//	}
	
}

